package com.showtao.front.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.showtao.front.domain.Catalog;

public interface CatalogRepository extends JpaRepository<Catalog, Long> {

    @Query("select distinct c from Catalog c " +
    		" left join c.children r " +
            "   left join r.children " +
            " where c.parent is null order by c.weight asc")
    List<Catalog> findTops();
    
    @Query("select distinct c from Catalog c " +
    		" left join c.parent" +
            " where c.parent is null order by c.weight asc")
    List<Catalog> findSimpleTops();
    
    @Query("select c from Catalog c " +
            " left join c.parent")
    Page<Catalog> findAllLeftJoinParent(Pageable pageable);
    
    @Query("select c from Catalog c " +
            " left join c.parent " +
    		" where c.alias = :alias")
    Catalog getByAliasLeftJoinParent(@Param("alias") String alias);
    
    @Query("select distinct c from Catalog c " +
            " left join c.parent p" +
            " left join c.children" +
    		" where p.id = :parentId")
    List<Catalog> findByParentId(@Param("parentId") Long parentId);
    
    @Query("select distinct c from Catalog c " +
            " left join c.parent p" +
            " left join c.children" +
            " where p.alias = :parentAlias")
    List<Catalog> findByParentAlias(@Param("parentAlias") String parentAlias);
}
